package com.bw.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bw.bean.DataBean;
import com.bw.service.DataBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DataBeanController {

    @Autowired
    private DataBeanService dataBeanService;


    @RequestMapping("/s1")
    public JSONObject select1(){
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");
        //业务数据查询
        DataBean dataBean = dataBeanService.select1();
        result.put("data",dataBean.getCta());
        return result;
    }


    @RequestMapping("/s2")
    public JSONObject select2(){
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");
        //创建对象
        JSONObject data = new JSONObject();
        result.put("data",data);

        //data中的属性
        JSONArray columns = new JSONArray();
        JSONArray rows = new JSONArray();

        data.put("columns",columns);
        data.put("rows",rows);

        JSONObject col1 = new JSONObject();
        col1.put("id","name");
        col1.put("name","题目名称");

        JSONObject col2 = new JSONObject();
        col2.put("id","cta");
        col2.put("name","正确答题次数");

        JSONObject col3 = new JSONObject();
        col3.put("id","ctb");
        col3.put("name","答题次数");

        JSONObject col4 = new JSONObject();
        col4.put("id","ctc");
        col4.put("name","正确率");

        columns.add(col1);
        columns.add(col2);
        columns.add(col3);
        columns.add(col4);

        //查询业务数据
        List<DataBean> beans = dataBeanService.select2();
        for (DataBean bean : beans) {
            //创建JSONObject对象
            JSONObject row = new JSONObject();
            row.put("name",bean.getName());
            row.put("cta",bean.getCta());
            row.put("ctb",bean.getCtb());
            row.put("ctc",bean.getCtc());
            //加入到rows中
            rows.add(row);
        }

        return result;
    }



    @RequestMapping("/s3")
    public JSONObject select3(){
        JSONObject result = new JSONObject();

        result.put("status",0);
        result.put("msg","");
        //创建对象
        JSONObject data = new JSONObject();
        result.put("data",data);

        //data中的属性
        JSONArray columns = new JSONArray();
        JSONArray rows = new JSONArray();

        data.put("columns",columns);
        data.put("rows",rows);


        JSONObject col0 = new JSONObject();
        col0.put("id","id");
        col0.put("name","序号");


        JSONObject col1 = new JSONObject();
        col1.put("id","name");
        col1.put("name","试卷名称");

        JSONObject col2 = new JSONObject();
        col2.put("id","cta");
        col2.put("name","考试人次");

        JSONObject col3 = new JSONObject();
        col3.put("id","ctb");
        col3.put("name","平均分");

        JSONObject col4 = new JSONObject();
        col4.put("id","ctc");
        col4.put("name","平均时长");

        columns.add(col0);
        columns.add(col1);
        columns.add(col2);
        columns.add(col3);
        columns.add(col4);

        //查询业务数据
        List<DataBean> beans = dataBeanService.select3();
        for (DataBean bean : beans) {
            //创建JSONObject对象
            JSONObject row = new JSONObject();
            row.put("name",bean.getName());
            row.put("cta",bean.getCta());
            row.put("ctb",bean.getCtb());
            row.put("ctc",bean.getCtc());
            row.put("id",bean.getId());
            //加入到rows中
            rows.add(row);
        }

        return result;
    }


    @RequestMapping("/s4")
    public JSONObject select4(){
        JSONObject result = new JSONObject();
        result.put("status",0);
        result.put("msg","");

        JSONObject data = new JSONObject();
        result.put("data",data);

        JSONArray mapData = new JSONArray();
        data.put("mapData",mapData);

        //业务数据查询
        List<DataBean> beans = dataBeanService.select4();
        for (DataBean bean : beans) {
            JSONObject row = new JSONObject();
            row.put("name",bean.getName());
            row.put("value",bean.getCta());

            JSONArray tooltipValues = new JSONArray();
            row.put("tooltipValues", tooltipValues);
            tooltipValues.add(bean.getCtb());
            tooltipValues.add(bean.getCtc());

            mapData.add(row);
        }

        data.put("valueName","订单总额");
        JSONArray tooltipNames = new JSONArray();
        data.put("tooltipNames", tooltipNames);

        tooltipNames.add("下单人数");
        tooltipNames.add("订单数");

        JSONArray tooltipUnits = new JSONArray();
        data.put("tooltipUnits", tooltipUnits);

        tooltipUnits.add("人");
        tooltipUnits.add("个");



        return result;
    }
}
